package com.west.xu.tdmq.schedule;

import com.west.xu.tdmq.domain.Notice;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

//@EnableScheduling
@Component
public class ProduceMsg {

    private static final Log logger = LogFactory.getLog(ProduceMsg.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss:sss");

    @Autowired
    private Producer<Notice> noticeProducer;

    //    @Scheduled(fixedRate = 10000)
    public void taskSendMsg() throws PulsarClientException {
        String msg = "现在时间：" + dateFormat.format(new Date());
        Notice notice = new Notice(RandomStringUtils.randomNumeric(6), msg);
        MessageId mssageId = noticeProducer.send(notice);
        logger.info("发送消息===>" + notice.toString() + "|MessageId===>" + mssageId.toString());
    }
}
